package com.blockchain.console.servlets.content.ptxg;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.blockchain.common.enums.IsPass;
import com.blockchain.common.enums.PlatformType;
import com.blockchain.console.service.content.ArticleManage;
import com.blockchain.console.service.content.entity.Article;
import com.blockchain.console.servlets.content.AbstractContentServlet;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.http.upload.FileStore;
import com.blockchain.framework.http.upload.PartFile;
import com.blockchain.framework.http.upload.UploadFile;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.util.StringHelper;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.util.parser.IntegerParser;
import com.blockchain.util.parser.TimestampParser;

@Right(id = "C_CONTENT_PTXG_UPDATE", isMenu = true, name = "平台相关")
public class UpdatePtxg extends AbstractContentServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void processPost(final HttpServletRequest request, final HttpServletResponse response,
			final ServiceSession serviceSession) throws Throwable {
		try {
			PlatformType p = EnumParser.parse(PlatformType.class, request.getParameter("type"));
			if (p == null) {
				p = PlatformType.PTJJ;
			}
			ArticleManage manage = serviceSession.getService(ArticleManage.class);
			Article article = new Article() {

				@Override
				public String getTitle() {
					PlatformType p = EnumParser.parse(PlatformType.class, request.getParameter("type"));
					String title = p.getName();
					if (StringHelper.isEmpty(title)) {
						title = PlatformType.PTJJ.getName();
					}
					return title;
				}

				@Override
				public String getSummary() {
					return request.getParameter("summary");
				}

				@Override
				public String getSource() {
					return request.getParameter("source");
				}

				@Override
				public int getSortIndex() {
					return IntegerParser.parse(request.getParameter("sortIndex"));
				}

				@Override
				public IsPass getPublishStatus() {
					return IsPass.S;

				}

				@Override
				public UploadFile getImage() throws Throwable {
					Part part = request.getPart("image");
					if (part == null || part.getContentType() == null || part.getSize() == 0) {
						return null;
					}
					return new PartFile(part);
				}

				@Override
				public String getContent() {
					return getResourceProvider().getResource(FileStore.class).encode(request.getParameter("content"));
				}

				@Override
				public Timestamp publishTime() {
					return TimestampParser.parse(request.getParameter("publishTime"));
				}

				@Override
				public IsPass getIstj() {
					return null;
				}

				@Override
				public int getIsorder() {
					return 0;
				}

				@Override
				public String getTitleEn() {
					return null;
				}

				@Override
				public String getTitleTw() {
					return null;
				}

				@Override
				public String getContentEn() {
					return getResourceProvider().getResource(FileStore.class).encode(request.getParameter("contentEn"));
				}

				@Override
				public String getContentTw() {
//					return getResourceProvider().getResource(FileStore.class).encode(request.getParameter("contentTw"));
					return "test";
				}
			};
			int id = manage.getId(p.name());
			if (id == 0) {
				manage.add(p.name(), article);
			} else {
				manage.update(id, article, p.name());
			}

			processGet(request, response, serviceSession);
		} catch (ParameterException | LogicalException e) {
			prompt(request, response, PromptLevel.ERROR, e.getMessage());
			processGet(request, response, serviceSession);
		}

	}

	@Override
	protected void processGet(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		PlatformType p = EnumParser.parse(PlatformType.class, request.getParameter("type"));
		if (p == null) {
			p = PlatformType.PTJJ;
		}
		ArticleManage manage = serviceSession.getService(ArticleManage.class);
		String content = manage.getContent(p.name());
		String contentEn = manage.getContentEn(p.name());
		String contentTw = manage.getContentTw(p.name());
		request.setAttribute("type", p);
		request.setAttribute("content", content);
		request.setAttribute("contentEn", contentEn);
		request.setAttribute("contentTw", contentTw);
		forwardView(request, response, getClass());
	}

}
